<!-- 合同列表 -->
<!-- 合同列表 -->
<!-- 合同列表 -->
<template>
  <div>
    <myDivider name="合同管理"></myDivider>

    <!-- 顶部搜索 -->
    <div class="top">
      <el-select clearable style="width: 150px;" v-model="keySelect" @change="getTableList" placeholder="筛选合同类型">
        <el-option v-for="item, i in selectData" :key="i" :label="item.name" :value="item.id"></el-option>
      </el-select>
      <el-date-picker @change="typeChange" style="width: 250px;cursor: pointer;" v-model="keyTime" type="daterange"
        unlink-panels :range-separator="null" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy.MM.dd">
      </el-date-picker>
      <el-select clearable style="width: 180px;" @change="keytitle = ''" v-model="keyType" placeholder="请选择筛选类别">
        <el-option label="按照合同名称查询" value="name"></el-option>
        <el-option label="按照小区名称查询" value="residentialName"></el-option>
        <el-option label="按照合同编号查询" value="contractNumber"></el-option>
        <el-option label="按照甲方名称查询" value="partyA"></el-option>
        <el-option label="按照乙方名称查询" value="partyB"></el-option>
      </el-select>
      <el-input class="width1" v-model="keytitle" placeholder="输入关键字查询"></el-input>
      <el-button type="primary" @click="SouSuo">查询</el-button>
      <el-button @click="reset">重置</el-button>
      <div style="margin-top: 10px;">
        <el-button type="primary" @click="$router.push('/contract/contractAdd?type=add')">新增</el-button>
        <el-button @click="HTMB">合同模板</el-button>
      </div>
    </div>

    <!-- 表格列表 -->
    <div class="tableBox">
      <el-table :data="tableData" style="width: 100%;margin-top: 50px;">
        <el-table-column align="center" prop="residentialName" label="小区名称" width="160">
        </el-table-column>
        <el-table-column align="center" prop="contractNumber" label="合同编号" width="140">
        </el-table-column>
        <el-table-column align="center" prop="name" label="合同名称">
        </el-table-column>
        <el-table-column align="center" prop="categoryName" label="类型" width="120">
        </el-table-column>

        <el-table-column align="center" prop="partyA" label="甲方名称" width="150">
        </el-table-column>
        <el-table-column align="center" prop="partyB" label="乙方名称" width="150">
        </el-table-column>
        <el-table-column align="center" prop="signingDate" label="签订日期" width="100">
        </el-table-column>
        <el-table-column align="center" prop="updateUserName" label="最后修改人员" width="150">
        </el-table-column>
        <el-table-column align="center" label="操作" width="160">
          <template slot-scope="scope">

            <div class="textBtn">
              <el-row>
                <el-col class="el" :span="8">
                  <span style="color: #5fadff;cursor: pointer;font-weight: 600;"
                    @click="Preview('detail', scope.row)">预览</span>
                </el-col>
                <el-col class="el" :span="8">
                  <span style="color: #5fadff;cursor: pointer;font-weight: 600;"
                    @click="Preview('update', scope.row)">修改</span>
                </el-col>
                <el-col class="el" :span="8">
                  <span style="color: #5fadff;cursor: pointer;font-weight: 600;"
                    @click="Preview('delete', scope.row)">删除</span>
                </el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="roomName" label="房间信息" width="200">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" :content="scope.row.roomName" placement="top-start">
              <span> {{
                scope.row.roomName.length > 8 ? '...' + scope.row.roomName.slice(scope.row.roomName.length - 8) :
                scope.row.roomName
              }}</span>
            </el-tooltip>
          </template> 
        </el-table-column> -->
      </el-table>
    </div>

    <!-- 分页器 -->
    <myPagination :pageSize="pageSize" :total="total" @PageChange="PageChange" @pageSizeChange="pageSizeChange">
    </myPagination>
    <!-- 所有弹窗 -->
    <!-- 合同模板 -->
    <el-dialog title="合同模板" :visible.sync="dialogVisible" width="350px">
      <div class="htmbBox">
        <div>模板下载：</div>
        <div>
          <div class="htmbBox_list">
            <div class="el">
              <span>
                房屋租赁合同.docx
              </span>
              <div>
                <el-button size="mini" type="primary" icon="el-icon-download" @click="dow(1)"></el-button>
              </div>
            </div>
            <div class="el">
              <span>
                装修管理服务合同.docx
              </span>
              <div>
                <el-button size="mini" type="primary" icon="el-icon-download" @click="dow(2)"></el-button>
              </div>
            </div>
            <div class="el">
              <span>
                房屋买卖合同.doc
              </span>
              <div>
                <el-button size="mini" type="primary" icon="el-icon-download" @click="dow(3)"></el-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data() {
    return {
      dialogVisible: false,
      //顶部筛选
      keySelect: '',
      keyType: '',
      selectData: [
        { name: '外包合作合同', id: '1' },
        { name: '物业服务合同', id: '2' },
        { name: '装修监管合同', id: '3' },
        { name: '房屋租售合同', id: '4' },
      ],
      keyTime: ['', ''],
      keytitle: '',
      // 表格数据
      tableData: [],
      // 分页器
      total: 1,
      pageNum: 1,
      pageSize: 10,
    }
  },
  async activated() {
    this.keySelect = ''
    this.keyTime = ['', '']
    this.keyType = ''
    this.keytitle = ''
    this.getTableList()
  },
  deactivated() {

  },
  methods: {
    dow(val) {
      let url
      let name
      switch (val) {
        case 1:
          url = defaultUrl+`/business/contract/downloadAnnex?filename=/default/contract/房屋租赁合同.docx`
          name = '房屋租赁合同.docx'
          break
        case 2:
          url =defaultUrl+ `/business/contract/downloadAnnex?filename=/default/contract/装修管理服务合同.docx`
          name = '装修管理服务合同.docx'
          break
        case 3:
          url = defaultUrl+`/business/contract/downloadAnnex?filename=/default/contract/房屋买卖合同.doc`
          name = '房屋买卖合同.doc'
          break
      }
      let soa_account = sessionStorage.getItem("soa_account");
      const token = sessionStorage.getItem("soa_token")
      axios({
        method: 'get',
        url,
        headers: {
          // 在这里添加自己需要的请求头
          account: soa_account,
          token: token,
        },
        responseType: 'blob'
      }).then(res => {
        const url = window.URL.createObjectURL(new Blob([res.data]))
        const a = document.createElement('a')
        a.href = url
        a.setAttribute('download', name)
        document.body.appendChild(a)
        a.click();
        document.body.removeChild(a)
      })
    },
    HTMB() {   //合同模板点击
      this.dialogVisible = true
    },
    typeChange(e) {
      this.getTableList()
    },
    async Preview(formName, row) { //表格按钮
      console.log(row, '所在行');
      switch (formName) {
        case 'detail': //点击预览
          this.$router.push('/contract/contractDetail?id=' + row.id)
          break
        case 'update': //点击修改
          if (row.checkUpdate) {
            this.$router.push('/contract/contractAdd?type=update&id=' + row.id)
          } else {
            this.$message.error('您无权修改，请联系超级管理员');
          }
          break
        case 'delete':
          if (row.checkDelete) { //可以删除
            this.$confirm('此操作将永久删除该合同, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(async () => {
              let res = await this.$api.contract.getdeleteContractLogic({
                id: row.id
              })
              if (res.code == 200) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
                this.getTableList()
              } else {
                this.$message.error(res.message);
              }
            }).catch(() => {

            });

          } else {//不可删除
            this.$message.error('不可删除');
          }


          break
      }
    },
    popOk(formName) { //弹窗确认接口
      switch (formName) {

      }
    },
    popClose(formName) { //弹窗取消按钮
      switch (formName) {

      }
    },
    popCloseChange(formname) { // 弹窗每次关闭时的函数
      switch (formname) {
      }
    },

    SouSuo() {
      this.pageNum = 1
      this.getTableList()
    },
    reset() {
      this.keySelect = ''
      this.keyTime = ['', '']
      this.keyType = ''
      this.keytitle = ''
      this.pageNum = 1
      this.getTableList()
    },
    pageSizeChange(e) { //分页器——每页数量变化
      this.pageSize = e
      this.getTableList()
    },
    PageChange(e) {//分页器——页数变化
      this.pageNum = e
      this.getTableList()
    },
    async getTableList() { // 获取表格列表
      if (this.keyTime == null) {
        this.keyTime = ['', '']
      }
      let userData = JSON.parse(sessionStorage.getItem('logOnUser'))
      let params = {
        "unitType": userData.unitType, //单位分类  机关agency  机构company 必传
        "unitId": userData.unitId, //单位id
        "category": this.keySelect, //合同类别
        "start": this.keyTime[0], //开始时间
        "end": this.keyTime[1], //结束时间
        "type": this.keyType,
        "keywords": this.keytitle, //关键字
        "pageSize": this.pageSize,
        "pageNum": this.pageNum,
      }
      let res = await this.$api.contract.getContractList(params)
      if (res.data.list.length < 1 && this.pageNum != 1) { //判断，当搜索关键字有内容时，我直接点击分页，会造成关键词列表中没有某某页的数据，那么我就用第一页重新请求一次
        this.pageNum = 1
        this.getTableList()
        return
      }
      this.total = res.data.total
      this.tableData = res.data.list
      console.log(res.data.list, '列表数据');
    },
  },
}
</script>

<style lang="less" scoped>
.textBtn {
  position: relative;

  /deep/ .el-row {
    display: flex;
    justify-content: center;
  }

  .el {
    display: block;
    position: relative;

    &:nth-child(n+2) {
      &::after {
        position: absolute;
        left: -1px;
        top: 50%;
        content: '';
        width: 2px;
        transform: translateY(-50%) scale(0.5, 1);
        height: 10px;
        background-color: #aaa;
      }
    }
  }
}

.width1 {
  width: 200px;
}

.htmbBox {
  display: flex;

  >div {
    &:nth-child(1) {
      width: 30%;
    }

    &:nth-child(2) {
      flex: 1;

      .htmbBox_list {
        width: 100%;

        .el {
          width: 100%;
          display: flex;
          justify-content: space-between;
          margin-bottom: 15px;
        }
      }
    }
  }
}

.top {
  width: 100%;
  padding: 20px 30px;
  box-sizing: border-box;
  position: relative;

}
</style>